package com.hundsun.hsOffer.common;

import com.hundsun.hsOffer.controller.HsOfferController;
import com.hundsun.hsOffer.controller.IndexController;
import com.hundsun.hsOffer.controller.SystemController;
import com.hundsun.hsOffer.controller.UserController;
import com.hundsun.hsOffer.freemarker.CompanyDirective;
import com.hundsun.hsOffer.freemarker.DictDirective;
import com.hundsun.hsOffer.intercept.SysLogInterceptor;
import com.hundsun.hsOffer.utils.NObugUtil;
import com.hundsun.hsOffer.utils.UpFileRenamePolicy;
import com.jagregory.shiro.freemarker.ShiroTags;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.Sqlite3Dialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.FreeMarkerRender;
import com.jfinal.render.ViewType;
import com.jfinal.upload.OreillyCos;
import org.apache.log4j.Logger;

/**
 * 核心控制类
 */

public class HsOfferConfig extends JFinalConfig {
    private Logger logger = Logger.getLogger(HsOfferConfig.class);
    private Routes routes;
    public void configConstant(Constants me) {
        PropKit.use("config.properties");
        me.setViewType(ViewType.FREE_MARKER);
        me.setBaseViewPath("/WEB-INF/views");
        me.setFreeMarkerViewExtension(".ftl");
        me.setError403View("/unauthorized.ftl");
        me.setDevMode(true);
    }

    /**
     * 配置路由
     *
     * @param me
     */
    public void configRoute(Routes me) {
        me.add("/", IndexController.class);
        me.add("/offer", HsOfferController.class, "/offer");
        me.add("/sys", SystemController.class, "/sys");
        me.add("/user", UserController.class,"/user");
        this.routes = me;
    }

    /**
     * 插件
     *
     * @param me
     */
    public void configPlugin(Plugins me) {
        DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"),
                PropKit.get("user"), PropKit.get("password"));
        druidPlugin.setDriverClass("org.sqlite.JDBC");
        me.add(druidPlugin);
        // ActiveRecord
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setDialect(new Sqlite3Dialect()); // ָ配置 Dialect
        _MappingKit.mapping(arp);
        arp.setShowSql(true);
        me.add(arp);
        // shiro插件
        ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);
        me.add(shiroPlugin);
        //ehcache 插件
        me.add(new EhCachePlugin());
    }

    /**
     * 拦截器
     *
     * @param me
     */
    public void configInterceptor(Interceptors me) {
        me.add(new ShiroInterceptor());
        //me.addGlobalActionInterceptor(new SysLogInterceptor());
    }
    public void configHandler(Handlers me) { me.add(new ContextPathHandler("ctx")); }

    /**
     *freemarker
     */
    public void afterJFinalStart() {
        super.afterJFinalStart();
        //设置文件上传重命名策略
        OreillyCos.setFileRenamePolicy(new UpFileRenamePolicy());
        FreeMarkerRender.getConfiguration().setSharedVariable("shiro",new ShiroTags());
        FreeMarkerRender.getConfiguration().setSharedVariable("dict",new DictDirective());
        FreeMarkerRender.getConfiguration().setSharedVariable("company",new CompanyDirective());
        logger.info(NObugUtil.goodLuck());
    }

    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 80, "/", 5);
    }

}